<?php

class ShopModel
{
    private $diaryListCacheModel;
    private $autoUidModel;
    private $paperType              =   0;
    private $shopKeyPrefix          =   'DPP/';
    private $shopZipKeyPrefix       =   'DPZ/';
    private $shopUserKeyPrefix      =   'DUP/';
    private $groupCatsKeyPrefix     =   'GCT/';
    private $activityKeyPrefix      =   'ACT/';
    private static $author          =   array();
    public function __construct(){
        $this->diaryListCacheModel  =   new DiaryListCacheModel();
        $this->autoUidModel         =   new AutoUidModel();
    }
    
    //================================================数据库统一处理============================================

	/**
	 * 获取个性装扮主页缓存
	 * @return mixed
	 */
	public function getShopDisguise(){
    	return $this->diaryListCacheModel->getShopDisguise();
	}

	/**
	 * 设置个性装扮主页缓存
	 * @param $data
	 */
	public function setShopDisguise($data){
		$this->diaryListCacheModel->setShopDisguise($data);
	}
    /**
     * 获取手账分类列表
     * @param number $id
     * @param unknown $start
     * @param unknown $length
     * @retunr unknown
     */
    public function getPlannerCategories($type)
    {
        $res = $this->diaryListCacheModel->getPlannerCategoryCacheList($type);
        if($res){
            foreach($res as $v){
                $tmp['cid'] = $v;
                $result[]=$tmp;
            }
            return $result;
        }

        $key    =   $this->groupCatsKeyPrefix . 0;
        $where  =   array('type'=>$type,'status'=>0);
        $orderBy=   ' display DESC ';
        $result =   Storage::getConditionManyData($key, $where, $orderBy);
        if($result){
            foreach($result as $val){
                $this->diaryListCacheModel->addPlannerCategoryCache($type,$val['cid']);
            }
        }
        return $result;
    }

    /**
     * 获取手账分类详情
     * @param unknown $cid
     * @return Ambigous <multitype:, unknown, boolean, mixed>
     */
    public function getPlannerCategoryInfo($cid, $type, $hashkey){
        $result = $this->diaryListCacheModel->getMysqlDataHashCache($hashkey, $cid);
        if (empty($result)){
            $key    =   $this->groupCatsKeyPrefix . 0;
            $where  =   array('status' => 0, 'cid' => $cid, 'type'=>$type);
            $result =   Storage::getConditionData($key, $where);
            $this->diaryListCacheModel->setMysqlDataHashCache($hashkey, $cid, $result);
        }
        return $result;
    }

    /**
     * 商店列表
     * @param unknown $goodsType 商品类型
     * @param number $start
     * @param number $length
     * @param number $id
     * @return Ambigous <unknown, boolean, multitype:>
     */
    public function getShopListByGoodsType ($goodsType = 0, $start = 0, $length = 20, $id = 0, $classification = 'all') {
        $key    =   $this->shopKeyPrefix. 0;
        $where  =   " status = 0 AND type = ".$goodsType;
        
        // 分页
        if ($id > 0){
            $where .= " AND id < ".$id;
        }
        
        // 排序
        $orderBy    =   ' id DESC ';
        
        // 根据客户端要求加分类
        $conditions = $this->checkShopListByClassification($classification, $where, $orderBy);
        $where      = $conditions['where'];
        $orderBy    = $conditions['orderBy'];
        
        $result     =   Storage::getConditionManyData($key, $where, $orderBy, $start, $length);
        return $result;
    }
    
    /**
     * 商品分类查看
     * @param unknown $classification
     * @param unknown $where
     * @param unknown $orderBy
     * @return multitype:string
     */
    private function checkShopListByClassification ($classification, $where, $orderBy) {
        // 分类
        switch ($classification) {
            case 'hot':
                $orderBy = " price_final DESC,sold_count DESC ";
                break;
            case 'new':
                $orderBy = " time DESC ";
                break;
            case 'free':
                $where   .= " AND price_final = 0 ";
                $orderBy = " sold_count DESC ";
                break;
        }
        
        $result = array('where' => $where, 'orderBy' => $orderBy);
        return $result;
    }
    
    /**
     * 获取商品详情
     * @param unknown $id
     * @param unknown $goodsType
     * @param unknown $redisKey
     * @return multitype:|Ambigous <boolean, multitype:, unknown, mixed>
     */
    public function getGoodsDetailByGoodsType ($id, $goodsType, $redisKey) {
        if ($id < 1 || empty($redisKey)) {
            return array();
        }
        
        $result = $this->diaryListCacheModel->getMysqlDataHashCache($redisKey, $id);
        if (empty($result) || count($result) < 10){
            $key      =   $this->shopKeyPrefix. 0;
            $where    =   array('id' => $id, 'status' => 0, 'type' => $goodsType);
            $result   =   Storage::getConditionData($key, $where);
            if (empty($result)){
                return array();
            }
    
            if ($result['status'] == 0){
                $this->diaryListCacheModel->setMysqlDataHashCache($redisKey, $id, $result);
            }
        }
        return  $result;
    }
    /*
     * 手账素材详情
     */
    public function getPlannerDetailByType($id,$goodsType){
        $key      =   $this->shopKeyPrefix. 0;
        $where    =   array('id' => $id, 'status' => 0, 'type' => $goodsType);
        $result   =   Storage::getConditionData($key, $where);
        if (empty($result)){
            return array();
        }
        return $result;
    }
    /*
     * 手账素材列表
     */
    public function getPlannerByType($uid,$goodsType, $redisKey){
        $data = $this->diaryListCacheModel->getPlannerCacheByType($redisKey);
        if (empty($data)){
            $key      =   $this->shopKeyPrefix. 0;
            $where    =   array('status' => 0, 'type' => $goodsType);
            $orderBy  =  " id DESC ";
            $result   =   Storage::getConditionManyData($key, $where, $orderBy);
            if (empty($result)){
                return array();
            }
            foreach ($result as $value){
                $tmp = $this->assembledShopListDataByGoodsType($uid, $value, $goodsType);
                $res[]=$tmp;
            }
            $string = serialize($res);
            $this->diaryListCacheModel->setPlannerCacheByType($redisKey,$string);
        }else{
            $res = unserialize($data);
        }
        
        return  $res;
    }
    
    /**
     * 获取我购买的商品列表
     * @param unknown $uid
     * @param number $goodsType
     * @param number $start
     * @param number $length
     * @param number $id
     * @return multitype:|Ambigous <unknown, boolean, multitype:>
     */
    public function getShopListMeBuyByGoodsType ($uid, $goodsType = 0, $start = 0, $length = 20, $id = 0) {
        if (empty($uid)){
            return array();
        }
    
        $key    =   $this->shopUserKeyPrefix. $uid;
        $where  =   " uid = ".$uid." AND status = 0 AND pid_type = ".$goodsType;
    
        // 分页
        if ($id > 0){
            $start = 0;
            $where .= ' AND pid < '.$id;
        }
    
        $orderBy=   ' pid DESC ';
        $result =   Storage::getConditionManyData($key, $where, $orderBy, $start, $length);
        return $result;
    }
    
    /**
     * 获取我购买的所有商品列表
     * @param unknown $uid
     * @return multitype:|Ambigous <unknown, boolean, multitype:>
     */
    public function getShopIdListMeBuy ($uid) {
        if (empty($uid)){
            return array();
        }
    
        $key    =   $this->shopUserKeyPrefix. $uid;
        $where  =   array('uid' => $uid, 'status' => 0);

        $orderBy=   ' pid DESC ';
        $result =   Storage::getConditionManyData($key, $where, $orderBy);
        if (empty($result)) {
            return array();
        }
        
        $shopIdList = array();
        foreach ($result as $value) {
            $shopIdList[] = $value['pid'];
        }
        return $shopIdList;
    }
    
    /**
     * 获取用户购买的单个商品的信息
     * @param unknown $uid
     * @param unknown $pid
     * @param unknown $goodsType
     * @return boolean|multitype:|Ambigous <multitype:, unknown, boolean, mixed>
     */
    public function getShopUserDetailByGoodsType($uid, $pid, $goodsType){
        if (empty($pid) || empty($uid)){
            AC::$errorNo    =   'DLM007';
            return FALSE;
        }
    
        $key    =   $this->shopUserKeyPrefix. $uid;
        $where  =   array('uid' => $uid, 'pid' => $pid, 'status'=>0, 'pid_type' => $goodsType);
        $result	=   Storage::getConditionData($key, $where);
        if (empty($result)) {
            return array();
        }
        
        return $result;
    }
   
    public function getShopUserByGoodsType($uid,$goodsType){
         $key    =   $this->shopUserKeyPrefix. $uid;
         $where  =   array('uid' => $uid,'status'=>0, 'pid_type' => $goodsType);
         $result	=   Storage::getConditionManyData($key, $where);
         if (empty($result)) {
             return array();
         }
        
        return $result;
    }
    /**
     * 购买商品
     * @param unknown $uid
     * @param unknown $id
     * @param unknown $goodsType
     * @param unknown $dateline
     * @return boolean
     */
    public function userbuyShopByGoodsType ($uid, $id, $goodsType, $dateline = 0){
        if (empty($id) || empty($uid)){
            AC::$errorNo    =   'DLM007';
            return false;
        }
    
        $data = array(
                'uid'            =>    $uid,
                'pid'            =>    $id,
                'pid_type'       =>    $goodsType,
                'time'           =>    AC::SERVER_TIME,
                'expire_time'    =>    0,
                'dateline'       =>    $dateline
        );
    
        $key    =   $this->shopUserKeyPrefix. $uid;
        $result =   Storage::addData($key, $data);
        if (!$result){
            return FALSE;
        }
    
        $this->increaseSaleAmount($id, $goodsType);
        return TRUE;
    }
    
    /**
     * 购买量增1
     * @param unknown $pid
     * @return Ambigous <boolean, number, resource>
     */
    public function increaseSaleAmount($id){
        if ($id < 1) {
            return FALSE;
        }
        
        $key    =   $this->shopKeyPrefix. 0;
        $data   =   array('sold_count'    =>    1);
        $where  =   array('id' => $id);
        $result =   Storage::changeNumValueAdd($key, $data, $where);
        return  $result;
    }
    
    /**
     * 修改有效期
     * @param unknown $uid
     * @param unknown $pid
     * @param unknown $dateline
     * @return boolean
     */
    public function setShopUserData ($uid, $pid, $dateline){
        if (empty($pid) || empty($uid)){
            AC::$errorNo    =   'DLM007';
            return false;
        }
    
        $key    = $this->shopUserKeyPrefix. $uid;
        $data   = array('dateline' => $dateline);
        $where  = array('uid'      => $uid, 'pid' => $pid);
        $result = Storage::setConditionData($key, $data, $where);
        if (!$result){
            return FALSE;
        }
    
        return TRUE;
    }
    
    //=====================================购买处理=====================================
    /**
     * 扣金币
     * @param unknown $amount
     * @param unknown $id
     * @param unknown $remark
     * @return boolean
     */
    public function payForShopByGoodsType ($uid, $amount, $id, $goodsType, $contract = FALSE){
        $result = FALSE;

        $remark_arr = array(
                0    =>    'paper_'.$id,
                1    =>    'emotion_'.$id,
                2    =>    'font_'.$id,
                5    =>    'skin_'.$id,
		6    =>    'tags_'.$id,
		7    =>    'plannerPaper_'.$id,
		8    =>	   'stickers_'.$id,
		9    =>    'brushs_'.$id,
		10   =>	   'templet_'.$id,
        11   =>    'frames_'.$id,
        12   =>    'plugins_'.$id,
		13   =>     'bootmgrs_'.$id,
			16   =>     'desk_'.$id,	
		17	 =>     'card_'.$id
        );
        $remark = $remark_arr[$goodsType];
        $amount = intval($amount);
        $id     = intval($id);
        
        if ($amount < 1 || $id < 1 || empty($remark)){
            return FALSE;
        }
        
        // 续费
        if ($contract) {
            $remark .= "_renewals";
        }

        $api = UK::getMallApi($uid);
        $coin = $api->executeCoinTask(MallApi::$shop_exchange, $amount, $remark);
        if ($coin && !empty($coin['change'])){
            $result = TRUE;
        }
    
        return $result;
    }
    
    /**
     * 检测是否已经购买过商品
     * @param unknown $id
     * @param unknown $goodsType
     * @return boolean
     */
    public function checkShopGoodsIsPaid($uid, $id, $goodsType){
        $result = FALSE;
        
        $remark_arr = array(
        		0    =>    'paper_'.$id,
        		1    =>    'emotion_'.$id,
        		2    =>    'font_'.$id,
        		5    =>    'skin_'.$id,
        		6    =>    'tags_'.$id,
        		7    =>    'plannerPaper_'.$id,
        		8    =>	   'stickers_'.$id,
        		9    =>    'brushs_'.$id,
        		10   =>	   'templet_'.$id,
                11   =>    'frames_'.$id,
                12   =>    'plugins_'.$id,
				13   =>    'bootmgrs_'.$id,
					16   =>     'desk_'.$id,
				17   =>    'card_'.$id
        );
        
        $remark = $remark_arr[$goodsType];
        if (empty($remark)) {
            return $result;
        }
        
        $api = UK::getMallApi($uid);
        $coinEchangeLog = $api->getCoinExchangeLog($remark);
        if (!empty($coinEchangeLog['results'])){
            $result = TRUE;
        }
        return $result;
    }
    
    //========================================任务及判断======================================
    /**
     * 任务数据结构
     * @param unknown $uid
     * @param unknown $task
     * @return multitype:string
     */
    public function taskOutPutData($uid, $task, $own = 0, $getCoin = TRUE){
        // 任务描述
        $assignType = array(
                1    =>    'level',
                2    =>    'ability',
                3    =>    'pay',
				4    =>    'member'
        );
        $task       = json_decode($task, true);
        $data = array(
                'type'    =>    strval($task['type']),
                'num'     =>    strval($task['num']),
                'desc'    =>    strval($task['desc']),
                'down'    =>    '0'
        );
        if ($uid > 0) {
            if ($assignType[$task['type']]){
                $method = "is".$assignType[$task['type']]."Check";
            
                if ($method == 'ispayCheck' && !$getCoin) {
                    $check  = FALSE;
                }else{
                    $check  = $this->$method($uid, $task['num']);
                }
                if ($check) $data['down'] = '1';
            }
            
            if ($own == 1) {
                $data['down'] = '1';
            }
        }
        $data  = $this->compatibleMember($data);
        return $data;
    }

    //兼容老版本无会员任务类型
	public function compatibleMember($data){
		if(AC::$member && $data && $data['type']=='4'){
			$data= (Object) array();
		}
		return $data;
	}
    /**
     * 验证入口
     * @param unknown $uid
     * @param unknown $info
     * @return boolean
     */
    public function checkTask ($uid, $info) {
        // 信纸类型及用户信息判断,1按等级,2按是否是达人
        $assignType = array(
                1    =>    'level',
                2    =>    'ability',
                3    =>    'pay',
		4    =>     'member'
        );
    
        // 任务限制
        $task_decode = json_decode($info['task'], TRUE);
        if ($task_decode && $assignType[$task_decode['type']]){
            $type   = $task_decode['type'];
            $num    = $task_decode['num'];
            $method = "is".$assignType[$type]."Check";
            $check  = $this->$method($uid, $num);
            if (!$check){
                if(AC::$member && $type==4){
                    AC::$errorMsg = '会员专属，请升级至最新版';
                    AC::$errorNo = 'DCP004';
                }
                if ($type != 3){
                    AC::$errorNo = 'DCP004';
                }
                return FALSE;
            }
        }
    
        return TRUE;
    }
    
    /**
     * 处理过期时间
     * @param unknown $time
     * @param unknown $dateline
     * @param unknown $period
     * @return multitype:number boolean
     */
    public function handleContractDateline ($time, $dateline, $period) {
        $dataline = 0;
        $contract = FALSE;
        if ($period > 0) {
            $dateline = AC::SERVER_TIME + 2592000 * $period;
        }
        if ($time > 0 && $dateline > 0 && $period > 0){
            $contract = TRUE;
        }
        $result = array(
            'dateline'  =>  $dataline,
            'contract'  =>  $contract
        );
        return $result;
    }

    public function ismemberCheck()
	{
		$uid = AC::HOST_UID;
		$user = $this->autoUidModel->getUserInfo($uid);
		if($user && $user['is_vip']){
			return true;
		}
		return false;
	}
    /**
     * 验证等级
     * @param unknown $uid
     * @param unknown $num
     * @return boolean
     */
    public function islevelCheck($uid, $num){
        if (AC::$hostUserBaseInfo['level'] < $num){
            return FALSE;
        }
        return TRUE;
    }
    
    /**
     * 验证达人
     * @param unknown $uid
     * @param unknown $num
     * @return boolean
     */
    public function isabilityCheck($uid, $num){
        if (empty(AC::$hostUserBaseInfo['is_ability'])){
            return FALSE;
        }
        return TRUE;
    }
    
    /**
     * 验证粉币
     * @param unknown $uid
     * @param unknown $num
     * @return boolean
     */
    public function ispayCheck($uid, $num){
        $userTotalCoin = $this->autoUidModel->getUserCoinInfo();
        if ($userTotalCoin >= $num){
            return TRUE;
        }
        return FALSE;
    }
    
    //========================================修复信纸、表情分类排序，启用缓存redis的list方案=========================================
    /**
     * 获取商品列表
     * @param unknown $redisKey
     * @param unknown $goodsType
     * @param unknown $sort
     * @param unknown $id
     * @param unknown $start
     * @param unknown $length
     * @return Ambigous <Ambigous, multitype:, multitype:>
     */
    public function getShopIdList ($redisKey, $goodsType, $sort, $id, $start, $length) {
        $result = $this->diaryListCacheModel->getShopListData($redisKey, $sort, $id, $start, $length);
        if (empty($result) && $id == 0) {
            $result = array();
            $list   = $this->getShopListByTypeAndSort($sort, $goodsType);
            if (!empty($list)) {
                foreach ($list as $value) {
                    $this->addShopListData($redisKey, $sort, $value['id']);
                    $result[] = $value['id'];
                }
                $result = array_slice($result, 0, $length);
            }
        }
        
        return $result;
    }
    
    /**
     * 数据库获取相应列表，用于redis的list重建
     * @param unknown $sort
     * @param unknown $goodsType
     * @param number $start
     * @param number $length
     * @return Ambigous <boolean, multitype:, unknown>
     */
    public function getShopListByTypeAndSort ($sort, $goodsType, $start = 0, $length = 0) {
        $key    =   $this->shopKeyPrefix. 0;
        $sql    =   "SELECT * FROM xxtLike.shop_diarypaper_0 WHERE status = 0 AND type = $goodsType ORDER BY id DESC";
        switch ($sort) {
            case 'hot':
                // 热门排序按照收费表情中下载量排序，收费排完之后排免费
                $sql    =   "SELECT *,IF(price_final > 0, 10000000, 1) + sold_count as r FROM xxtLike.shop_diarypaper_0 WHERE status = 0 AND type = $goodsType ORDER BY r DESC";
                break;
            case 'new':
                // 完全按照上架时间先后排序，包括收费和免费
                $sql    =   "SELECT * FROM xxtLike.shop_diarypaper_0 WHERE status = 0 AND type = $goodsType ORDER BY time DESC";
                break;
            case 'free':
                // 所有免费表情按照下载量排序
                $sql    =   "SELECT * FROM xxtLike.shop_diarypaper_0 WHERE status = 0 AND type = $goodsType AND price_final = 0 ORDER BY sold_count DESC";
                break;
            case 'vip':
                // 所有Vip表情按照下载量排序
                $sql    =   "SELECT * FROM xxtLike.shop_diarypaper_0 WHERE status = 0 AND type = $goodsType AND vip = 1 ORDER BY sold_count DESC";
                break;
            default:
                $sql    =   "SELECT *,IF(price_final > 0, 10000000, 1) + sold_count as r FROM xxtLike.shop_diarypaper_0 WHERE status = 0 AND type = $goodsType ORDER BY r DESC";
                break;
        }
        if ($start > 0 || $length > 0) $sql .= " LIMIT $start, $length ";
        
        $result =   Storage::fetchDbSentenceByClient($key, $sql, 2);
        return $result;
    }

    /**
     * 添加商品进redis的商品列表
     * @param unknown $redisKey
     * @param unknown $sort
     * @param unknown $id
     * @return unknown
     */
    public function addShopListData($redisKey, $sort, $id) {
        $result = $this->diaryListCacheModel->addShopListData($redisKey, $sort, $id);
        return $result;
    }
    
    /**
     * 请求皮肤包列表
     * @param unknown $platform
     * @param unknown $goodsType
     * @param unknown $version
     * @param number $start
     * @param number $length
     * @return Ambigous <boolean, multitype:, unknown>
     */
    public function getShopZipList ($platform, $goodsType, $version) {
        
        $key    =   $this->shopZipKeyPrefix. 0;
        $sql    =   "SELECT * FROM xxtLike.shop_diarypaper_zip_0 WHERE `status` = 0 AND `platform` = '".$platform."' AND `type` = ".$goodsType." AND '".$version."' >= `min_version` AND '".$version."' <= `max_version` ORDER BY id DESC";
        
        $result =   Storage::fetchDbSentenceByClient($key, $sql, 2);
        return $result;
    }
    
    public function getPaginationDataOfShopZipList ($id, $length, $list) {
        if (empty($list)) return array();
        if (empty($id)) return array_slice($list, $id, $length);
        
        $index = 0;
        foreach ($list as $key=>$value) {
        	if ($id == $value['pid']) {
        		$index = $key;
        		break;
        	}
        }
        
        $list = array_slice($list, ($index + 1), $length);
        return $list;
    }
    
    /**
     * 获取皮肤包链接详情
     * @param unknown $pid
     * @param unknown $platform
     * @return multitype:|Ambigous <multitype:, unknown, boolean>
     */
    public function getShopZipInfo ($id, $platform, $version) {
    
        $key    =   $this->shopZipKeyPrefix. 0;
        $sql    =   "SELECT * FROM xxtLike.shop_diarypaper_zip_0 WHERE `status` = 0 AND `pid` = '".$id."' AND `platform` = '".$platform."' AND '".$version."' >= `min_version` AND '".$version."' <= `max_version`";
        
        $result =   Storage::fetchDbSentenceByClient($key, $sql);
        if (empty($result)) {
            return array();
        }
        
        return $result;
    }
    
    /**
     * 添加皮肤包
     * @param unknown $param
     * @return boolean|multitype:|Ambigous <unknown, boolean, number, resource>
     */
    public function addShopZip ($param) {
        if (empty($param['pid']) || empty($param['zip_url'])) return FALSE;
        
        $data   =   $this->assembleSkinZipDataOfMysql($param, 'add');
        $key    =   $this->shopZipKeyPrefix. 0;
        $result	=   Storage::addData($key, $data);
        if (empty($result)) {
            return array();
        }
        
        return $result;
    }
    
    /**
     * 修改包信息
     * @param unknown $id
     * @param unknown $data
     * @return multitype:|Ambigous <boolean, unknown, number, resource>
     */
    public function editShopZipInfo ($id, $data) {
        if (empty($id) || empty($param['pid']) || empty($param['zip_url'])) return FALSE;
        
        $data   =   $this->assembleSkinZipDataOfMysql($param);
        $key    =   $this->shopZipKeyPrefix. 0;
        $where  =   array('id' => $id);
        $result	=   Storage::setConditixonData($key, $data, $where);
        if (empty($result)) {
            return array();
        }
    
        return $result;
    }
    
    public function checkShopZipValid($version, $shopZipInfo) {
        if (empty($shopZipInfo) || empty($shopZipInfo['zip_url'])) return FALSE;
        
        $check_min_version = version_compare($version, $shopZipInfo['min_version'], '>=');
        $check_max_version = version_compare($version, $shopZipInfo['max_version'], '<=');
        if (!$check_min_version || !$check_max_version) return FALSE;
        
        return TRUE;
    }
    
    //========================================组装数据=========================================
    /**
     * 商品列表数据
     * @param unknown $uid
     * @param unknown $data
     * @param unknown $goodsType
     * @return Ambigous <string, multitype:string multitype:string  StdClass multitype:multitype:string   Ambigous <multitype:string, multitype:string > Ambigous <multitype:multitype:string, multitype:multitype:string Ambigous <string>  > >
     */
    public function assembledShopListDataByGoodsType ($uid, $data, $goodsType) {
        $result = array(
                'id'             =>    '',
                'name'           =>    '',
                'desc'           =>    '',
                'label'          =>    '0',
                'cover_s'        =>    '',
                'cover'          =>    '',
                'price_orign'    =>    '0',
                'price_final'    =>    '0',
                'price_desc'     =>    '',
                'copyright'      =>    '@粉粉日记',
                'category'       =>    '0',
                'status'         =>    '0',
                'expire_time'    =>    '0',
                'dateline'       =>    '0',
                'use_time'      =>    array(
                        'ptime' => '0',
                        'desc'  => '永久有效'
                ),
                'tag'            =>    array(),
                'name_tag'       =>    array(
                        'bg'    =>    '0',
                        'text'  =>    '静态资源'
                ),
                'task'           =>    (Object) array(),
                'own'            =>    '0',
                'download_url'   =>    '',
        		'thumbnail'      =>	   array(),
        		'author'        =>    array(
        				'uid'            =>    '3',
        				'nickname'       =>    '粉粉设计酱',
        				'introduction'   =>    '小小兔工作室',
        				'avatar'         =>    AC::getProtocolUriByOS(AC::$cdn_url_icon).'/A7/BA/F3/3/3_5ddd3c49c5.png!icon'
        		),             
        		'data'		 =>   (Object)array(),
                'kind'       =>  '',
                'hidden'    =>  0,
	
        );
        
        // 组装返回数据(不需要特殊处理,直接返回替换字段后的数据)
        UK::assembleReplacedFieldData($data, $result, array(
                                                            'id'          =>    'id',
                                                            'name'        =>    'name',
                                                            'desc'        =>    'desc',
                                                            'label'       =>    'label',
                                                            'status'      =>    'status',
                                                            'cover_s'     =>    'cover_s',
                                                            'cover'       =>    'cover',
                                                            'category'    =>    'category',
                                                            'zip_url'     =>    'download_url',
                                                            'price_desc'  =>    'price_desc',
                                                            'expire_time' =>    'expire_time',
                                                            'price_first' =>    'price_orign',
                                                            'price_final' =>    'price_final'
                                                ));
        //打包的json数据处理
        $result['data'] = $this->assemTemData($data);

        //判断是否有引导器种类
        $result['kind'] = $this->getBootmgrKind($data);
        //判断是否隐藏
        $result['hidden'] = $this->getTempletDisplayStatus($data);
        // 判断是否已购买
        if ($uid > 0 && !in_array($goodsType,array('6','7','8','9','10','11','12','13'))) {
            $shopUser = $this->getShopUserDetailByGoodsType($uid, $result['id'], $goodsType);
            if ($shopUser) {
                $result['own'] = '1';
                $result['dateline'] = strval($shopUser['dateline']);
            }
        }
        
        // 返回并组装商品有效期
        if ($data['period'] > 0) {
            $result['use_time'] = array(
                    'ptime'    =>    strval($data['period']),
                    'desc'     =>    '可使用'.$data['period'].'个月'
            );
        }
        
        // 返回商品购买条件
        if(!empty($data['task'])){

			$result['name']   = $this->compatibleMemberName($data);
			$result['desc']   = $this->compatibleMemberDesc($data);

            // TODO 待优化
            $types = array(5,6,7,8,9,16);
            $getCoin = (in_array($goodsType,$types)) ? true : false;
            $result['task']   = $this->taskOutPutData($uid, $data['task'], 0, $getCoin);
        }else{
            $result['task'] = $this->plannerDefaultTaskByVersion();
        }
        
        // 返回标签数据
        $result['tag'] = $this->assembleTagData($data['tag']);
        
        // 判断图片类别
        if (!empty($data['name_tag'])){
            $result['name_tag']['bg']      =    strval($data['name_tag']);
            if ($data['name_tag'] == 1){
                $result['name_tag']['text']    =    '动态资源';
            }
        }
		
        $result['thumbnail'] = $this->assembleThumbnailDataByGoodsType($data['thumbnail'], $goodsType, $result['id'], $result['download_url']);
        $result['author']	 = $this->assembleAuthorData($data['cuid']);
        $result['download_url'] = AC::getProtocolUriByOS($result['download_url']);
        $result['cover'] = AC::getProtocolUriByOS($result['cover']);
        $result['cover_s'] = AC::getProtocolUriByOS($result['cover_s']);
		$result['cover'] = $this->getnewFontCover($result['cover'],$goodsType);
		$result['cover_s'] = $this->getnewFontCover($result['cover_s'],$goodsType);
        return $result;
    }

    public function getnewFontCover($cover,$goodsType)
	{
		if(!AC::$fontCoverVersion && $goodsType == '2'){
			//http://d.fenfenriji.com/diary/font/30026/profile/cover.png
			$a=explode('/',$cover);
			$l = count($a);
			$a[$l-1]='newcover.png';
			return join($a,'/');
		}
		if(AC::$fontCoverVersion && $goodsType == '2'){
			//http://d.fenfenriji.com/diary/font/30026/profile/cover.png
			$a=explode('/',$cover);
			$l = count($a);
			$a[$l-1]='cover.png';
			return join($a,'/');
		}
		return $cover;
	}
    public function getBootmgrKind($data){
    	return trim($data['background']);
    }
    
    public function getTempletDisplayStatus($data){
        return $data['vip'];
    }

    public function plannerDefaultTaskByVersion(){   	
    	if(AC::$plannerVersion==1){
    		$data = array(
    				'type'    =>    0,
    				'num'     =>    0,
    				'desc'    =>    '',
    				'down'    =>    '0'
    		);
    		return $data;
    	}
    	return (Object)array();
    }
    public function assemTemData($data){
    	if(empty($data['extra'])){
    		return (Object)array();
    	}
    	
    	$res = array(
    			'width' 	=> '',
    			'height'	=> '',
    			'paper' 	=>(Object)array(),
    			'brushs'	=>array(),
    			'widgets'	=>array(),
            'android_version' => '',
            'ios_version' => ''
    	);
    
    		$data['extra'] = UK::repalceStr($data['extra']);	
    		if(UK::isJSON($data['extra'])){
    			$extra  = json_decode($data['extra'],true); 			 
    			if(!empty($extra['data'])){
    				$data = $extra['data'];
    				$res = array(
    						'width' 	=> empty($data['width']) ? 0 : $data['width'],
    						'height'	=> empty($data['height']) ? 0 : $data['height'],
    						'paper' 	=>empty($data['paper']) ? (Object)array() : $data['paper'],
    						'brushs'	=>empty($data['brushs']) ?  array() : $data['brushs'],
    						'widgets'	=>empty($data['widgets']) ? array() : $data['widgets'],
                        'android_version'=>empty($data['android_version']) ? '' : $data['android_version'],
                        'ios_version'=>empty($data['ios_version']) ? '' : $data['ios_version'],
    				);
    				 
    			}
    		}
    
    	return $res;
    }
    /**
     * 商品详情数据
     * @param unknown $uid
     * @param unknown $data
     * @param unknown $goodsType
     * @return multitype:string multitype: multitype:string  multitype:multitype:string   Ambigous <multitype:string, multitype:string > Ambigous <multitype:multitype:string, multitype:multitype:string Ambigous <string>  > Ambigous <multitype:, Ambigous, multitype:, string, multitype:string > unknown
     */
    public function assembledShopDetailDataByGoodsType ($uid, $data, $goodsType) {
        $result = array(
                'id'            =>    '',
                'name'          =>    '',
                'desc'          =>    '',
                'cover'         =>    '',
                'cover_s'       =>    '',
                'thumbnail'     =>    array(),
                'background'    =>    '',
                'expire_time'   =>    '0',
                'expire_desc'   =>    '永久有效',
                'dateline'       =>    '0',
                'use_time'      =>    array(
                        'ptime' => '0',
                        'desc'  => '永久有效'
                ),
                'label'         =>    '0',
                'category'      =>    '0',
                'zip_size'      =>    '',
                'copyright'     =>    '@粉粉日记',
                'sale_amount'   =>    '',
                'price_orign'   =>    '',
                'price_final'   =>    '',
                'price_desc'    =>    '',
                'status'        =>    '0',
                'tag'           =>    array(
                        array('bg' => 'new',  'text' => '最新'),
                        array('bg' => 'free', 'text' => '免费'),
                ),
                'name_tag'      =>    array(
                        'bg'      =>    '0',
                        'text'    =>    '静态资源'
                ),
                'task'          =>    array(),
                'author'        =>    array(
                        'uid'            =>    '1',
                        'nickname'       =>    '粉粉管理员',
                        'introduction'   =>    '小小兔工作室',
                        'avatar'         =>    AC::getProtocolUriByOS(AC::$cdn_url_icon).'/75/84/9B/1/1.png'
                ),
                'own'           =>    '0',
                'download_url'  =>    '',
        	'data'		=>	  array(),
        );
        
        // 组装返回数据(不需要特殊处理,直接返回替换字段后的数据)
        UK::assembleReplacedFieldData($data, $result, array(
                                         'id'          =>    'id',
                                         'name'        =>    'name',
                                         'desc'        =>    'desc',
                                         'cover'       =>    'cover',
                                         'label'       =>    'label',
                                         'status'      =>    'status',
                                         'cover_s'     =>    'cover_s',
                                         'category'    =>    'category',
                                         'zip_size'    =>    'zip_size',
                                         'zip_url'     =>    'download_url',
                                         'price_desc'  =>    'price_desc',
                                         'background'  =>    'background',
                                         'sold_count'  =>    'sale_amount',
                                         'expire_time' =>    'expire_time',
                                         'expire_desc' =>    'expire_desc',
                                         'price_first' =>    'price_orign',
                                         'price_final' =>    'price_final'
                                    ));
  
        //打包的json数据处理
        $result['data'] = $this->assemTemData($data);
        // 判断是否已购买
        if ($uid > 0) {
            $shopUser = $this->getShopUserDetailByGoodsType($uid, $result['id'], $goodsType);
            if ($shopUser) {
                $result['own'] = '1';
                $result['dateline'] = strval($shopUser['dateline']);
            }
        }
        
        // 返回并组装商品有效期
        if ($data['period'] > 0) {
            $result['use_time'] = array(
                    'ptime'    =>    strval($data['period']),
                    'desc'     =>    '可使用'.$data['period'].'个月'
            );
        }
        
        // 返回商品购买条件
        if(!empty($data['task'])){
			$result['name']   = $this->compatibleMemberName($data);
            $result['task']   = (array)$this->taskOutPutData($uid, $data['task']);
        }
        
        // 达到条件模拟下载
        if (intval($result['task']['down']) > 0 && empty($result['own']) && intval($result['task']['type']) != 3){
            $this->userbuyShopByGoodsType($uid, $result['id'], $goodsType);
            $result['own'] = '1';
        }

        // 返回标签数据
        $result['tag'] = $this->assembleTagData($data['tag']);
        
        // 判断图片类别
        if (!empty($data['name_tag'])){
            $result['name_tag']['bg']      =    strval($data['name_tag']);
            if ($data['name_tag'] == 1){
                $result['name_tag']['text']    =    '动态资源';
            }
        }
        
        // 组装返回的预览图列表
        $result['thumbnail'] = $this->assembleThumbnailDataByGoodsType($data['thumbnail'], $goodsType, $result['id'], $result['download_url']);

        // 返回资源作者信息
        $result['author'] = $this->assembleAuthorData($data['cuid']);
        //处理https
        $result['download_url'] = AC::getProtocolUriByOS($result['download_url']);
        $result['cover'] = AC::getProtocolUriByOS($result['cover']);
        $result['cover_s'] = AC::getProtocolUriByOS($result['cover_s']);

		$result['cover'] = $this->getnewFontCover($result['cover'],$goodsType);
		$result['cover_s'] = $this->getnewFontCover($result['cover_s'],$goodsType);

        if ($json = json_decode($data['task'], true)){
            if (AC::$member && UK::checkOS(AC::$appInfoList[2])=='2' && $json['type']=='4'){
                $result['download_url'] = '';
            }
        }

        return $result;
    }


	/**
	 * 老版本会员素材后加(会员专属)
	 * @param $name
	 */
    public function compatibleMemberName ($data)
	{
		$name = $data['name'];
		if(AC::$member && $data['task']){
				$task       = json_decode($data['task'], true);
				if($task['type']=='4'){
						$name = $data['name'].'(会员专属)';
				}
		}
		return $name;
	}

    /**
     * 老版本会员素材后加(会员专属)
     * @param $name
     */
    public function compatibleMemberDesc ($data)
    {
        $desc = $data['desc'];
        if(AC::$member && $data['task']){
            $task       = json_decode($data['task'], true);
            if($data['type']==2 && $task['type']=='4'){//字体
                $desc = $data['desc'].'(会员专属)';
            }
        }
        return $desc;
    }
    /**
     * 组装zip包的数据格式
     * @param unknown $param
     * @param string $type
     * @return multitype:number string NULL
     */
    private function assembleSkinZipDataOfMysql ($param, $type = 'edit') {
        $data = array(
            'pid'           =>  0,
            'zip_size'      =>  '',
            'zip_url'       =>  '',
            'type'          =>  5,
            'platform'      =>  '',
            'min_version'   =>  '',
            'max_version'   =>  '',
            'status'        =>  0
        );
        
        isset($param['pid'])         && $data['pid']         = intval($param['pid']);
        isset($param['type'])        && $data['type']        = intval($param['type']);
        isset($param['status'])      && $data['status']      = intval($param['status']);
        isset($param['zip_size'])    && $data['zip_size']    = strval($param['zip_size']);
        isset($param['zip_url'])     && $data['zip_url']     = strval($param['zip_url']);
        isset($param['platform'])    && $data['platform']    = strval($param['platform']);
        isset($param['min_version']) && $data['min_version'] = strval($param['min_version']);
        isset($param['max_version']) && $data['max_version'] = strval($param['max_version']);
        
        if ($type === 'add') {
            $data['time'] = AC::SERVER_TIME;
        }
        
        return $data;
    }
    
    /**
     * 组装返回的标签数据
     * @param unknown $tag
     * @return multitype:multitype:string Ambigous <string>
     */
    private function assembleTagData ($tag) {
        $result = array();
        
        $text_arr = array(
                'new'    =>    '最新',
                'hot'    =>    '最热'
        );
        if (!empty($tag)){
            $tags = explode(',', $tag);
            foreach ($tags as $val){
                $result[] = array(
                        'bg'    =>    strval($val),
                        'text'  =>    $text_arr[$val]
                );
            }
        }else{
            if(AC::$iosPlannerTagVersion == 1){
                $result[] = array(
                        'bg'    =>    'normal',
                        'text'  =>    '正常'
                );
            }
        }
        
        return $result;
    }
    
    /**
     * 组装预览图数据
     * @param unknown $thumbnailJson
     * @param unknown $goodsType
     * @param unknown $id
     * @return multitype:|Ambigous <multitype:, string, multitype:string >
     */
    private function assembleThumbnailDataByGoodsType ($thumbnailJson, $goodsType, $id, $url='') {
		if ($goodsType==16){
			return '';
		}
        if (empty($thumbnailJson)) {
            return array();
        }
        $d_url = AC::getProtocolUriByOS(AC::$cdn_url_d);
        
        $result    = array();
        $thumbnail = json_decode($thumbnailJson, TRUE);
        // 组装信纸数据
        if ($goodsType == 0 || $goodsType == 3) {
            $tmp = array();
            $baseUrl = "{$d_url}/diary/paper/".$id."/priview/";
            
            foreach($thumbnail as $value){
                $tmp[] = $baseUrl.$value;
            }
            $result = implode(',', $tmp);
        }elseif ($goodsType == 1 || $goodsType == 4) {
            // 组装表情数据
            $tmp = array();
            $ename = $this->getSourceNameByZipName($url);
            $baseUrl_s = "{$d_url}/diary/emotion/".$id."/priview/".$ename."_";
            $baseUrl_m = "{$d_url}/diary/emotion/".$id."/".$ename."/".$ename."_";
            
            foreach($thumbnail as $value){
                $tmp = array(
                        's'    =>    $baseUrl_s.$value['s'],
                        'm'    =>    $baseUrl_m.$value['m'],
                );
                $thumbArr[] = $tmp;
            }
            $result = $thumbArr;
        }elseif ($goodsType == 2) {
            // 组装字体预览图数据
            $tmp = array();
            $baseUrl = "{$d_url}/diary/font/".$id."/priview/";
        
            foreach($thumbnail as $value){
                $tmp[] = $baseUrl.$value;
            }
            $result = implode(',', $tmp);
        }elseif ($goodsType == 5) {
            // 组装皮肤预览图数据
            $tmp = array();
            $priview = 'priview';
            if(!AC::$skinPriviewVersion){
                $priview = 'priview4.9';
            }
            $baseUrl = "{$d_url}/diary/appskin/android/".$id."/".$priview."/";
        
            foreach($thumbnail as $value){
                $tmp[] = $baseUrl.$value;
            }
            $result = implode(',', $tmp);
        }elseif($goodsType == 6){
            // 手账标签数据
            $tmp = array();
            $baseUrl = "{$d_url}/diary/planner/tags/".$id;
       
            foreach($thumbnail as $value){
                $tmp = array(
                    's'    =>    $baseUrl.'/priview/'.$value,
                    'm'    =>    $baseUrl.'/priview/'.$value,
                );
                $thumbArr[] = $tmp;
            }
            $result = $thumbArr;
        }elseif($goodsType == 7){
            // 手账背景数据
            $tmp = array();
            $baseUrl = "{$d_url}/diary/planner/paper/".$id;
            foreach($thumbnail as $value){
        		
                $tmp = array(
                    's'    =>    $baseUrl.'/priview/'.$value,
                    'm'    =>    $baseUrl.'/priview/'.$value,
                );
                $thumbArr[] = $tmp;
            }
            $result = $thumbArr;
        }elseif($goodsType == 8){
            // 手账贴纸数据
            $tmp = array();
            $baseUrl = "{$d_url}/diary/planner/stickers/".$id;
            foreach($thumbnail as $value){
                $arr_value=explode('_',$value);
                $tmp = array(
                    's'    =>    $baseUrl.'/priview/'.$value,
                    'm'    =>    $baseUrl.'/'.$arr_value[0].'/'.$value,
                );
                $thumbArr[] = $tmp;
            }
            $result = $thumbArr;
        }elseif($goodsType == 9){
            // 手账花边笔数据
            $tmp = array();
            $baseUrl = "{$d_url}/diary/planner/brushs/".$id;
            foreach($thumbnail as $value){
                $arr_value=explode('_',$value);
                $tmp = array(
                    's'    =>    $baseUrl.'/priview/'.$value,
                    'm'    =>    $baseUrl.'/'.$arr_value[0].'/'.$value,
                );
                $thumbArr[] = $tmp;
            }
            $result = $thumbArr;
        }elseif($goodsType == 11){
            // 手账边框数据
            $tmp = array();
            $baseUrl = "{$d_url}/diary/planner/frames/".$id;
            foreach($thumbnail as $value){
                $arr_value=explode('_',$value);
                $tmp = array(
                    's'    =>    $baseUrl.'/priview/'.$value,
                    'm'    =>    $baseUrl.'/'.$arr_value[0].'/'.$value,
                );
                $thumbArr[] = $tmp;
            }
            $result = $thumbArr;
        }elseif($goodsType == 12){
            // 手账插件数据
            $tmp = array();
            $baseUrl = "{$d_url}/diary/planner/plugins/".$id;

            foreach($thumbnail as $value){
                $tmp = array(
                    's'    =>    $baseUrl.'/priview/'.$value,
                    'm'    =>    $baseUrl.'/priview/'.$value,
                );
                $thumbArr[] = $tmp;
            }
            $result = $thumbArr;
        }
        
        return $result;
    }

    /**
     * 根据包链接获取资源名字
     * @param $url
     * @return array
     */
    private function getSourceNameByZipName($url){
        $arra = parse_url($url);
        $path = explode('/', $arra['path']);
        $pop  = array_pop($path);
        $m_name = explode('_',$pop);
        return $m_name[0];
    }
    
    /**
     * 组装商品作者信息
     * @param unknown $id
     * @param unknown $cUid
     * @return unknown
     */
    private function assembleAuthorData ($uid) {
        $uid = $uid ? $uid : 3;
        if(self::$author[$uid]){
           $user = self::$author[$uid]; 
        }else{
            $user = $this->autoUidModel->getUserInfo($uid);
            self::$author[$uid]=$user;
        }

        $user['introduction'] = $user['signature'];
        return $user;
    }
    
    /**
     * 解析表情数据
     * @param unknown $msgType
     * @param unknown $content
     * @return Ambigous <StdClass, Ambigous>
     */
    public function parseEmotionData ($msgType, $content) {
        // 匹配查找表情
        $msgData =array();
        $emotionData = Emotion::matchEmotionDetail($content);
        if ($emotionData && $emotionData['id'] > 0){
            $emotionInfo = $this->getGoodsDetailByGoodsType($emotionData['id'], 1, 'Shop-Emotion');
            if ($emotionInfo){
                $msgType = 'emotion';
                $msgData = array(
                    'eid'    =>    $emotionInfo['id'],
                    'etitle' =>    $emotionInfo['name'],
                    'eicon'  =>    $emotionInfo['cover_s'],
                    'image'  =>    $emotionData['image']
                );
                $param['msgData'] = json_encode($msgData);
            }
        }
        
        // 私信返回附加媒体信息
        $result = $this->restructMsgData($msgType, $param['msgData']);
        return $result;
    }
    
/**
     * 组装聊天返回数据中的附加媒体信息
     * @param unknown $msgType
     * @param unknown $msgData
     * @return StdClass|Ambigous <StdClass, multitype:string >
     */
    public function restructMsgData($msgType, $msgData){
        $dataArr = json_decode($msgData, true);
        if (!is_array($dataArr) || empty($msgData)){
            return (Object) array();
        }
    
        // 组装不同类型的返回结构
        $result = (Object) array();
        if (in_array($msgType, array('voice', 'image'))){
            $result = array(
                    'vfid'    =>    '0',
                    'tovfid'  =>    '0'
            );
    
            isset($dataArr['vfid'])     &&    $result['vfid']     =    strval($dataArr['vfid']);
            isset($dataArr['tovfid'])   &&    $result['tovfid']   =    strval($dataArr['tovfid']);
        }elseif ($msgType == 'emotion'){
            $result = array(
                    'eid'       =>    '0',
                    'etitle'    =>    '',
                    'eicon'     =>    '',
                    'image'     =>    '',
            );
    
            isset($dataArr['eid'])     &&    $result['eid']     =    strval($dataArr['eid']);
            isset($dataArr['etitle'])  &&    $result['etitle']  =    strval($dataArr['etitle']);
            isset($dataArr['eicon'])   &&    $result['eicon']   =    strval($dataArr['eicon']);
            isset($dataArr['image'])   &&    $result['image']   =    strval($dataArr['image']);
        }elseif ($msgType == 'event'){
            $result = array(
                    'image'     =>    '',
                    'action'    =>    '',
                    'eventType' =>    '',
            );
    
            isset($dataArr['image'])       &&    $result['image']       =    strval($dataArr['image']);
            isset($dataArr['action'])      &&    $result['action']      =    strval($dataArr['action']);
            isset($dataArr['eventType'])   &&    $result['eventType']   =    strval($dataArr['eventType']);
        }else{
            $result = array(
                    'image'     =>    '',
                    'action'    =>    '',
                    'title'     =>    '',
                    'content'   =>    '',
                    'tpl'   =>    '0',
            );
    
            isset($dataArr['image'])     &&    $result['image']     =    strval($dataArr['image']);
            isset($dataArr['action'])    &&    $result['action']    =    strval($dataArr['action']);
            isset($dataArr['title'])     &&    $result['title']     =    strval($dataArr['title']);
            isset($dataArr['content'])   &&    $result['content']   =    strval($dataArr['content']);
            isset($dataArr['tpl'])       &&    $result['tpl']       =    strval($dataArr['tpl']);
        }
    
        return $result;
    }

    //=====================================banner=====================================
    
    public function getShopDisguiseBannerData(){
        $banner =$this->bannerListByType('dress',10);
        if(!$banner){
            return array();
        }
        $result=array();
        foreach ($banner as $value){
            $tmp=array(
                'id'    =>'',
                'image' =>AC::getProtocolUriByOS($value['image_url']),
                'link'  =>'',
                'action'=>$value['action_url'],
            );
            $result[]=$tmp;
        }
        return $result;
    }
    public function getEmotionBannerData(){
        $url = AC::getProtocolUriByOS(AC::$cdn_url_img);
        $result = array(
                array('id' => 20037, 'image' => $url.'/70/17/23/Image/7DA611D3-935C-1D86-A01B-556EB8A2AE60.png', 'link' => '', 'action' => 'pinksns://mall/emotion/detail?eid=20037'),
                array('id' => 20036, 'image' => $url.'/70/17/23/Image/BD0C27F5-582A-528A-D17C-55517E385C3C.jpg', 'link' => '', 'action' => 'pinksns://mall/emotion/detail?eid=20036')
        );
        return $result;
    }
    
    public function getPaperBannerData(){
        $url = AC::getProtocolUriByOS(AC::$cdn_url_img);
        $result = array(
                array('id' => 10000, 'image' => $url.'/A4/C8/31/Image/ED5D0FA5-9C5C-71E3-4BD0-547C0442634B.jpg', 'link' => '', 'action' => 'pinksns://group/topic?gid=16&tid=2224&uid=4388392'),
        );
        return $result;
    }
    
    public function getFontBannerData(){
        $url = AC::getProtocolUriByOS(AC::$cdn_url_img);
        $result = array(
                array('id' => 30001, 'image' => $url.'/70/17/23/Image/2FC6D9B3-57F5-223F-64DA-550913FBF3A4.jpg', 'link' => '', 'action' => 'pinksns://mall/font/detail?fid=30001')
        );
        return $result;
    }
    
    public function getSkinBannerData(){
        $url = AC::getProtocolUriByOS(AC::$cdn_url_img);
        $result = array(
            array('id' => 40001, 'image' => $url.'/70/17/23/Image/2FC6D9B3-57F5-223F-64DA-550913FBF3A4.jpg', 'link' => '', 'action' => 'pinksns://mall/skin/detail?sid=40001')
        );
        return $result;
    }
    
    
    //===================================================old===============================================
    /**
     * 获取信纸列表
     * @param unknown $type
     * @param unknown $start
     * @param unknown $length
     * @param unknown $isAdmin
     * @return Ambigous <unknown, boolean, multitype:>
     */
    public function getPaperList($type = "new", $start, $length, $id = 0, $min_price = 0, $max_price = 0){
        $key    =   $this->shopKeyPrefix. 0;
        $where  =   " status = 0 AND tag = 'new' AND price_first = 0 AND type = ".$this->paperType;
        if ($type == "all"){
            $where  =   " status = 0 AND tag = 'new' AND type = ".$this->paperType;
        }
        if ($id > 0){
            $start = 0;
            $where .= " AND id < ".$id;
        }
    
        // 价格判断
        if ($min_price || $max_price > 0){
            $start = 0;
            $where = ' status  = 0 AND price_first >= '.$min_price.' AND price_first <= '.$max_price.' AND type = '.$this->paperType;
            if($id > 0){
                $where .= ' AND id < '.$id;
            }
        }
    
        $orderBy=   ' id DESC ';
        $result =   Storage::getConditionManyData($key, $where, $orderBy, $start, $length);
    
        return $result;
    }
    
    /**
     * 获取信纸详情
     * @param unknown $pid
     * @return multitype:|Ambigous <string, multitype:>
     */
    public function getPaperDetail($pid){
        // 取缓存
        $result = $this->diaryListCacheModel->getPaperDetailByIdCache($pid);
        if (empty($result) || count($result) < 10){
            $key    =   $this->shopKeyPrefix. 0;
            $where  =   array('id' => $pid,'status' => 0, 'type' => $this->paperType);
            $result	=   Storage::getConditionData($key, $where);
            if (empty($result)){
                return array();
            }
            if ($result['status'] == 0){
                $this->diaryListCacheModel->setDiaryPaperCache($pid, $result);
            }
        }
        $result = $this->paperDetailData($result, AC::HOST_UID);
        return $result;
    }
    
    /**
     * 获取我购买的信纸
     * @param unknown $uid
     * @param unknown $start
     * @param unknown $length
     * @return boolean|Ambigous <unknown, boolean, multitype:>
     */
    public function getMyPaperList($uid, $start, $length, $id){
        if (empty($uid)){
            AC::$errorNo = 'DM001';
            return false;
        }
    
        $key    =   $this->shopUserKeyPrefix . $uid;
        $where  =   array('uid'=>$uid,'status' => 0, 'pid_type' => $this->paperType);
        if ($id > 0){
            $start = 0;
            $where = ' pid < '.$id.' AND status  = 0 AND uid = '.$uid.' AND pid_type = '.$this->paperType;
        }
    
        $orderBy=   ' pid DESC ';
        $result =   Storage::getConditionManyData($key, $where, $orderBy, $start, $length);
    
        return $result;
    }
    
    /**
     * 获取我购买的单个信纸详情
     * @param unknown $uid
     * @param unknown $pid
     * @return boolean|Ambigous <multitype:, unknown, boolean, mixed>
     */
    public function getPaperUser($uid,$pid){
        if (empty($pid) || empty($uid)){
            AC::$errorNo    =   'DLM007';
            return false;
        }
    
        $key    =   $this->shopUserKeyPrefix. $uid;
        $where  =   array('uid' => $uid,'pid' => $pid,'status'=>0, 'pid_type' => $this->paperType);
        $result	=   Storage::getConditionData($key, $where);
    
        return $result;
    }
    
    /**
     * 购买信纸
     * @param unknown $uid
     * @param unknown $pid
     * @return boolean
     */
    public function userbuyPaper($uid, $pid, $dateline) {
        if (empty($pid) || empty($uid)){
            AC::$errorNo    =   'DLM007';
            return false;
        }
    
        $data = array(
                'uid'        =>    $uid,
                'pid'        =>    $pid,
                'status'     =>    0,
                'pid_type'   =>    $this->paperType,
                'expire_time'=>    0,
                'dateline'   =>    $dateline,
                'time'       =>    AC::SERVER_TIME
        );
    
        $key    =   $this->shopUserKeyPrefix. $uid;
        $result =   Storage::addData($key, $data);
        if (!$result){
            return false;
        }
    
        $this->increaseSaleAmount($pid);
        return true;
    }
    
    /**
     * 组装信纸列表数据结构
     * @param unknown $param
     * @param unknown $uid
     * @return Ambigous <string, multitype:>
     */
    public function paperListData($param,$uid){
        // 默认返回数据
        $data = array(
                'pid'            =>    '0',
                'name'           =>    '',
                'desc'   	 =>    '',
                'label'          =>    '0',
                'cover_s'        =>    '',
                'price_first'    =>    '',
                'price_second'   =>    '',
                'copyright'      =>    '©粉粉日记',
                'category'       =>    "",
                'tag'            =>    array(
                        'bg'    =>    '1',
                        'text'  =>    '最新'
                ),
                'name_tag'       =>    array(
                        'bg'    =>    '1',
                        'text'  =>    ''
                ),
                'task'           =>    array(),
                'own'            =>    '0',
                'download_url'   =>    ''
        );
         
        isset($param['id'])            &&    $data['pid']              =    strval($param['id']);
        isset($param['name'])          &&    $data['name']             =    strval($param['name']);
        isset($param['desc'])          &&    $data['desc']             =    strval($param['desc']);
        isset($param['label'])         &&    $data['label']            =    strval($param['label']);
        isset($param['cover_s'])       &&    $data['cover_s']          =    strval($param['cover_s']);
        isset($param['price_first'])   &&    $data['price_first']      =    strval($param['price_first']);
        isset($param['price_second'])  &&    $data['price_second']     =    strval($param['price_second']);
        isset($param['category'])      &&    $data['category']         =    strval($param['category']);
        isset($param['tag'])           &&    $data['tag']['bg']        =    strval($param['tag']);
        isset($param['name_tag'])      &&    $data['name_tag']['bg']   =    strval($param['name_tag']);
        isset($param['zip_url'])       &&    $data['download_url']     =    strval($param['zip_url']);
    
        // tag描述
        $tagArr = array(
                'new'    =>    "最新",
                'hot'    =>    "最热",
                'free'   =>    "免费"
        );
        $data['tag']['text']    =    $tagArr[$data['tag']['bg']];
    
        if ((AC::SERVER_TIME - intval($param['time']) - 2592000) > 0){
            $data['tag']['bg']   = '';
            $data['tag']['text'] = '';
        }
    
        if($param['task']){
            $taskData   = $this->taskOutPutData($uid, $param['task']);
            $data['task'] = $taskData;
        }
    
        $own = $this->getShopUserDetailByGoodsType($uid, $data['pid'], 0);
        if($own){
            $data['own'] = '1';
        }
    
        return $data;
    }
    
    public function paperDetailToListData($param,$uid){
        // 默认返回数据
        $data = array(
                'pid'            =>    '0',
                'name'           =>    '',
                'desc'   	 =>    '',
                'label'          =>    '0',
                'cover_s'        =>    '',
                'price_first'    =>    '',
                'price_second'   =>    '',
                'copyright'      =>    '©粉粉日记',
                'category'       =>    "",
                'tag'            =>    array(
                        'bg'    =>    '1',
                        'text'  =>    '最新'
                ),
                'name_tag'       =>    array(
                        'bg'    =>    '1',
                        'text'  =>    ''
                ),
                'task'           =>    array(),
                'own'            =>    '0',
                'download_url'   =>    ''
        );
         
        isset($param['pid'])               &&    $data['pid']              =    strval($param['pid']);
        isset($param['name'])              &&    $data['name']             =    strval($param['name']);
        isset($param['desc'])              &&    $data['desc']             =    strval($param['desc']);
        isset($param['label'])             &&    $data['label']            =    strval($param['label']);
        isset($param['cover_s'])           &&    $data['cover_s']          =    strval($param['cover_s']);
        isset($param['price_first'])       &&    $data['price_first']      =    strval($param['price_first']);
        isset($param['price_second'])      &&    $data['price_second']     =    strval($param['price_second']);
        isset($param['category'])          &&    $data['category']         =    strval($param['category']);
        isset($param['tag']['bg'])         &&    $data['tag']['bg']        =    strval($param['tag']['bg']);
        isset($param['tag']['text'])       &&    $data['tag']['text']      =    strval($param['tag']['text']);
        isset($param['name_tag']['bg'])    &&    $data['name_tag']['bg']   =    strval($param['name_tag']['bg']);
        isset($param['name_tag']['text'])  &&    $data['name_tag']['text'] =    strval($param['name_tag']['text']);
        isset($param['own'])               &&    $data['own']              =    strval($param['own']);
        isset($param['download_url'])      &&    $data['download_url']     =    strval($param['download_url']);
    
        if ((AC::SERVER_TIME - intval($param['time']) - 2592000) > 0){
            $data['tag']['bg']   = '';
            $data['tag']['text'] = '';
        }
    
        if($param['task']){
            $taskData   = $this->taskOutPutData($uid, $param['task']);
            $data['task'] = $taskData;
        }
        return $data;
    }
    
    /**
     * 组装信纸详情数据结构
     * @param unknown $param
     * @param unknown $uid
     * @return Ambigous <string, multitype:>
     */
    private function paperDetailData($param,$uid){
        // 默认返回数据
        $data = array(
                'pid'            =>    '0',
                'name'           =>    '',
                'desc'           =>    '',
                'cover'          =>    '',
                'cover_s'        =>    '',
                'thumbnail'      =>    '',
                'background'     =>    '',
                'expire_time'    =>    '-1',
                'expire_desc'    =>    '永久有效',
                'status'         =>    '0',
                'author'         =>    array(),
                'label'          =>    '0',
                'tag'            =>    array(
                        'bg'            =>    'new',
                        'text'          =>    '最新'
                ),
                'name_tag'       =>    array(
                        'bg'            =>    '1',
                        'text'          =>    '静态图'
                ),
                'task'           =>    array(),
                'zip_size'       =>    '0',
                'copyright'      =>    '粉粉日记',
                'category'       =>    "1",
                'sold_count'     =>    '0',
                'price_first'    =>    '0',
                'price_second'   =>    '0',
                'price_final'    =>    '0',
                'own'            =>    '0',
                'time'           =>    AC::SERVER_TIME,
                'download_url'   =>    ''
                        );
         
        isset($param['id'])                    &&    $data['pid']                  =    strval($param['id']);
        isset($param['name'])                  &&    $data['name']                 =    strval($param['name']);
        isset($param['desc'])                  &&    $data['desc']                 =    strval($param['desc']);
        isset($param['cover'])                 &&    $data['cover']                =    strval($param['cover']);
        isset($param['cover_s'])               &&    $data['cover_s']              =    strval($param['cover_s']);
        isset($param['background'])            &&    $data['background']           =    strval($param['background']);
        isset($param['expire_time'])           &&    $data['expire_time']          =    strval($param['expire_time']);
        isset($param['expire_desc'])           &&    $data['expire_desc']          =    strval($param['expire_desc']);
        isset($param['status'])                &&    $data['status']               =    strval($param['status']);
        isset($param['label'])                 &&    $data['label']                =    strval($param['label']);
        isset($param['tag'])                   &&    $data['tag']['bg']            =    strval($param['tag']);
        isset($param['name_tag'])              &&    $data['name_tag']['bg']       =    strval($param['name_tag']);
        isset($param['zip_size'])              &&    $data['zip_size']             =    strval($param['zip_size']);
        isset($param['zip_url'])               &&    $data['download_url']         =    strval($param['zip_url']);
        isset($param['sold_count'])            &&    $data['sold_count']           =    strval($param['sold_count']);
        isset($param['price_first'])           &&    $data['price_first']          =    strval($param['price_first']);
        isset($param['price_second'])          &&    $data['price_second']         =    strval($param['price_second']);
        isset($param['price_final'])           &&    $data['price_final']          =    strval($param['price_final']);
        isset($param['time'])                  &&    $data['time']                 =    strval($param['time']);

        // 判断是否已购买
        $shopUser = $this->getShopUserDetailByGoodsType($uid, $data['pid'], $this->paperType);
        if ($shopUser) {
            $data['own'] = '1';
        }
    
        // tag描述
        $tagArr = array(
                'new'    =>    "最新",
                'hot'    =>    "最热",
                'free'   =>    "免费"
        );
        $data['tag']['text']    =    $tagArr[$data['tag']['bg']];
    
        if ((AC::SERVER_TIME - intval($param['time']) - 2592000) > 0){
            $data['tag']['bg']   = '';
            $data['tag']['text'] = '';
        }
    
        if($param['task']){
            $taskData   = $this->taskOutPutData($uid, $param['task'], $data['own']);
            $data['task'] = $taskData;
        }
    
        // 可下载信纸，服务端模拟客户端调用一次下载
        if (intval($data['task']['down']) > 0 && empty($data['own']) && intval($data['task']['type']) != 3){
            $this->userbuyPaper($uid, $data['pid']);
            $data['own'] = '1';
        }
    
        // 组装返回的预览图列表
        $baseUrl = AC::getProtocolUriByOS(AC::$cdn_url_d)."/diary/paper/".$data['pid']."/priview/";
        if(isset($param['thumbnail'])){
            $thumbs = json_decode($param['thumbnail'], true);
            $tmp = array();
            foreach($thumbs as $value){
                $tmp[] = $baseUrl.$value;
            }
            $data['thumbnail'] = $tmp;
            $data['thumbnail'] = implode(",", $data['thumbnail']);
        }
    
        if (!empty($param['cUid'])) {
            $uid = intval($param['cUid']);
        }else{
            $uid = Emotion::getEmotionAuthorUid($data['pid']);
        }
    
        $user = $this->autoUidModel->getUserInfo($uid);
        $user = $this->autoUidModel->checkIsMe($user);
        $data['author'] = $user;
    
        return $data;
    }

    public function bannerListByType($type='', $len = 20){
        if (empty($type))return null;
        $key    =   $this->activityKeyPrefix. 0;
        $where  =   ['type' => $type];
        return Storage::getConditionManyData($key, $where, 'sort desc', 0, $len);
    }
}